package com.liang.cloud.Converter;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.liang.cloud.Dto.OrderDTO;
import com.liang.cloud.Emun.ResultEnum;
import com.liang.cloud.Entity.OrderDetail;
import com.liang.cloud.Exception.SellException;
import com.liang.cloud.Form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 余亮亮
 * @description:
 * @create: 2019-12-30 14:52:38
 **/
@Slf4j
public class OrderForm2OrderDTOConverter {

    public static OrderDTO convert(OrderForm orderForm) {
        Gson gson = new Gson();
        OrderDTO orderDTO = new OrderDTO();

        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());

        List<OrderDetail> orderDetailList = new ArrayList<>();
        try {
            //List<Object> obj=  gson.fromJson(字符串, new TypeToken<List<要转换的list集合的>>(){}.getType());
            orderDetailList = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>() {
                    }.getType());
        } catch (Exception e) {
            log.error("【对象转换】错误, string={}", orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        orderDTO.setList(orderDetailList);

        List<Integer> itemsId = new ArrayList<>();
        try {
            //List<Object> obj=  gson.fromJson(字符串, new TypeToken<List<要转换的list集合的>>(){}.getType());
            itemsId = gson.fromJson(orderForm.getItemsId(),
                    new TypeToken<List<Integer>>() {
                    }.getType());
        } catch (Exception e) {
            log.error("【对象转换】错误, string={}", orderForm.getItemsId());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        orderDTO.setCartList(itemsId);

        return orderDTO;
    }


}
